package com.example.flowermall.controller;

import com.example.flowermall.domain.WechatRequest;
import com.example.flowermall.domain.WechatResponse;
import com.example.flowermall.domain.WxPay;
import com.example.flowermall.entity.CreateOrder;
import com.example.flowermall.entity.Result;
import com.example.flowermall.entity.User;
import com.example.flowermall.service.IUserService;
import com.example.flowermall.service.WxService;
import com.example.flowermall.util.RedisUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;

@RequestMapping("/wx")
@RestController
public class WxPayController {
    private final WxService wxService;
    private final IUserService userService;
    private final RedisUtil redisUtil;
    @Autowired
    PasswordEncoder passwordEncoder;

    public WxPayController(WxService wxService, IUserService userService, RedisUtil redisUtil) {
        this.wxService = wxService;
        this.userService = userService;
        this.redisUtil = redisUtil;
    }

    //余额充值
    @GetMapping("/createWxOrder")
    public Result createWxOrder(WxPay wxPay) throws Exception {
        return wxService.createWxOrder(wxPay);
    }

    //余额购买
    @PostMapping("/balance")
    public Result balance(@RequestBody CreateOrder createOrder) {
        User user = userService.getUser(createOrder.getUserId());
        boolean matches = passwordEncoder.matches(createOrder.getPassword(), user.getPassword());
        if (!matches) {
            Object cacheObject = redisUtil.getCacheObject(createOrder.getUserId().toString());
            if (ObjectUtils.isEmpty(cacheObject)) {
                redisUtil.setCacheObject(createOrder.getUserId().toString(), 1, 10, TimeUnit.MINUTES);
            } else {
                int parseInt = Integer.parseInt(cacheObject.toString());
                if (parseInt >= 3) {
                    return Result.error("密码错误超过三次");
                }
                redisUtil.setCacheObject(createOrder.getUserId().toString(), parseInt + 1, 10, TimeUnit.MINUTES);
            }
            return Result.error("密码错误");
        }
        createOrder.setIsPay(1);
        return wxService.balance(createOrder);
    }

    //积分兑换
    @PostMapping("/integral")
    public Result integral(@RequestBody CreateOrder createOrder) {
        User user = userService.getUser(createOrder.getUserId());
        boolean matches = passwordEncoder.matches(createOrder.getPassword(), user.getPassword());
        if (!matches) {
            Object cacheObject = redisUtil.getCacheObject(createOrder.getUserId().toString());
            if (ObjectUtils.isEmpty(cacheObject)) {
                redisUtil.setCacheObject(createOrder.getUserId().toString(), 1, 10, TimeUnit.MINUTES);
            } else {
                int parseInt = Integer.parseInt(cacheObject.toString());
                if (parseInt >= 3) {
                    return Result.error("密码错误超过三次");
                }
                redisUtil.setCacheObject(createOrder.getUserId().toString(), parseInt + 1, 10, TimeUnit.MINUTES);
            }
            return Result.error("密码错误");
        }
        createOrder.setIsPay(1);
        return wxService.integral(createOrder);
    }

    @RequestMapping(value = "/callback",
            consumes = MediaType.TEXT_XML_VALUE,  // 接收 XML 请求
            produces = MediaType.TEXT_XML_VALUE)
    public ResponseEntity<WechatResponse> wxNotify(@RequestBody WechatRequest wechatRequest, HttpServletRequest request) throws Exception {
        return wxService.notifyWx(wechatRequest,request);
    }

}
